<?php

namespace app\api\controller;

use think\Db;

class Muyuxuan extends Base
{
    /**
     * 日常请求
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function daily(){
        $orderPositionId = $this->request->param('title');
//        echo '<pre>';
//        print_r('haha');
//        exit;\
        //获取位置
        $postion = Db::name('position')->where('positionNo',$orderPositionId)->find();
        if(empty($postion)){
            return json([
                'code'=>0,
                'msg'=>'该位置不存在',
                'data'=>null,
            ]);
        }
//        echo '<pre>';
//        print_r($postion);
//        exit;
        //更新当前打位状态
        $this->jiesuo($orderPositionId);
        $postion_state = Db::name('postion_state')->where('positionNo',$orderPositionId)->find();
        $mini_program_code = '';
        $qr_code = '';
        if(!empty($postion['mini_program_code'])){
            $mini_program_code = $this->request->domain().$postion['mini_program_code'];
        }
        if(!empty($postion['qr_code'])){
            $qr_code = $this->request->domain().$postion['qr_code'];
        }
        return json([
            'code'=>1,
            'msg'=>'获取成功',
            'data'=>[
                'status'=>$postion_state['status'],
                'mini_program_code'=>$mini_program_code,
                'qr_code'=>$qr_code,
            ]
        ]);
    }
    protected function jiesuo($orderPositionId){
        $strtime =strtotime(date("Y-m-d ").'00:00:00');
        //获取当前打位的状态表
        $postion_state = Db::name('postion_state')->where('positionNo',$orderPositionId)->find();
        if(empty($postion_state)){
            $postion_state_array = [
                'positionNo'=>$orderPositionId,
                'status'=>1,
                'createtime'=>time(),
                'updatetime'=>time(),
            ];
            Db::name('postion_state')->insert($postion_state_array);
            $postion_state = Db::name('postion_state')->where('positionNo',$orderPositionId)->find();
        }
//        $orders = Db::name('order_subscribe')
//            ->where('orderDate',$strtime)
//            ->where('orderStartTime','>=',date("H:i",time()))
//            ->where('orderEndTime','<=',date("H:i",time()))
//            ->where('orderPositionId',$orderPositionId)
//            ->where('orderStatus',1)
//            ->order('orderStartTime asc')
//            ->find();
//        echo '<pre>';
//        print_r($orders);
//        exit;
        $orders = Db::name('order_subscribe')
            ->where('orderDate',$strtime)
            ->where('orderStartTime','<=',date("H:i",time()+3600))
            ->where('orderEndTime','>=',date("H:i",time()))
            ->where('orderPositionId',$orderPositionId)
            ->where('orderStatus',1)
            ->order('orderStartTime asc')
            ->find();
        if(empty($orders)){
            //没有订单锁屏
            $res = Db::name('postion_state')->where('positionNo',$orderPositionId)->update(['status'=>1,'notice'=>0,'updatetime'=>time()]);
            //10分钟前有订单并且10分钟后有订单，并且是同一个人解锁
            $upOrder = Db::name('order_subscribe')
                ->where('orderDate',$strtime)
                ->where("orderEndTime",'between',[date("H:i",time()-600),date("H:i",time())])
                ->where('orderPositionId',$orderPositionId)
                ->where('orderStatus','<>',-1)
                ->order('orderEndTime','desc')
                ->find();
            $downOrder = Db::name('order_subscribe')
                ->where('orderDate',$strtime)
                ->where('orderPositionId',$orderPositionId)
                ->where("orderStartTime",'between',[date("H:i",time()),date("H:i",time()+600)])
                ->where('orderStatus','=',1)
                ->order('orderStartTime','asc')
                ->find();
            //10分钟前有订单并且10分钟后有订单，并且是同一个人解锁
            if(!empty($upOrder)&&!empty($downOrder)&&$upOrder['userId']==$downOrder['userId']){
                $res = Db::name('postion_state')->where('positionNo',$orderPositionId)->update(['status'=>5,'updatetime'=>time(),'notice'=>1]);
            }
        }else{
            $start_time = strtotime(date("Y-m-d ").$orders['orderStartTime']);
            $end_time = strtotime(date("Y-m-d ").$orders['orderEndTime']);
            //进行中订单
            if($start_time<time()&&$end_time>time()){
                if($end_time<time()+300){
                    if($postion_state['notice']!=1){
                        $res = Db::name('postion_state')->where('positionNo',$orderPositionId)->update(['status'=>6,'updatetime'=>time(),'notice'=>1]);
                    }else{
                        $res = Db::name('postion_state')->where('positionNo',$orderPositionId)->update(['status'=>5,'updatetime'=>time()]);
                    }
                }
            }
        }
        return true;
    }

    /**
     * 重新锁屏
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function lock_screen(){
        $orderPositionId = $this->request->param('title');
        $postion_state = Db::name('postion_state')->where('positionNo',$orderPositionId)
            ->find();
        if(empty($postion_state)){
            return json([
                'code'=>0,
                'msg'=>'该位置不存在',
                'data'=>null,
            ]);
        }
        $res = Db::name('postion_state')->where('positionNo',$orderPositionId)->update(['status'=>1,'updatetime'=>time(),'notice'=>0]);
        if(!$res){
            return json([
                'code'=>0,
                'msg'=>'锁屏失败',
                'data'=>null,
            ]);
        }
        return json([
            'code'=>1,
            'msg'=>'锁屏成功',
            'data'=>null,
        ]);
    }
}